package com.chen.demo.entity;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author Y·C
 * @version 1.0.0
 * @ClassName OptionalDemo.java
 * @Description TODO
 * @createTime 2023年05月06日 08:53:00
 */
public class OptionalDemo {
    public static void main(String[] args) {
//        Author author = getAuthor();
//        Optional<Author> authorOptional = Optional.ofNullable(author);
//        authorOptional.ifPresent(author1 -> System.out.println(author1.getName()));
//
//        Optional<Author> authorOptional1 = getAuthorOptional();
//        authorOptional1.ifPresent(author2 -> System.out.println(author2.getName()));

//        Optional<Author> authorOptional = getAuthorOptional();
//        Author author = authorOptional.orElseGet(() -> new Author(1L, "chen", 33, "一个从菜刀中明悟哲理的祖安人", null));
//        System.out.println(author.getName());

//        Optional<Author> authorOptional = getAuthorOptional();
//        try {
//            Author author = authorOptional.orElseThrow((Supplier<Throwable>) () -> new RuntimeException("数据为null"));
//            System.out.println(author);
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }

        testFilter();

    }

    private static void testFilter() {
        Optional<Author> authorOptional = getAuthorOptional();
        authorOptional.filter(author -> author.getAge()>18)
                .ifPresent(author -> System.out.println(author.getName()));

    }

    public static Optional<Author> getAuthorOptional(){
        Author author = new Author(1L, "梦多", 33, "一个从菜刀中明悟哲理的祖安人", null);
        return Optional.ofNullable(author);
    }

    public static Author getAuthor(){
        Author author = new Author(1L, "梦多", 33, "一个从菜刀中明悟哲理的祖安人", null);
        return author;
    }
}


